import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:imember_printer/product.dart';

export 'product.dart';

class TP809 {
  static const MethodChannel _channel = MethodChannel('tp_809');

  ///連接打印機
  static Future<void> connectPrinter({required String ipAddress, required String port}) async {
    await _channel.invokeMethod('connectPrinter', <String, String>{
      'ipAddress': ipAddress,
      'port': port,
    });
  }

  ///斷開打印機
  static Future<void> disconnectPrinter() async {
    await _channel.invokeMethod('disconnectPrinter', <String, String>{});
  }

  ///打印文本
  static Future<void> printText({required String text}) async {
    await _channel.invokeMethod('printText', <String, dynamic>{
      'text': text,
    });
  }

  ///打印圖片
  /// 1 inch = size:192
  /// 2 inch = size:384
  static Future<void> printImage({required String filePath, int size = 576}) async {
    await _channel.invokeMethod('printImage', <String, dynamic>{
      'filePath': filePath,
      'size': size,
    });
  }
}

class HT300 {
  static const MethodChannel _channel = MethodChannel('ht_300');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}

class ImemberPrinter {
  static const MethodChannel _channel = MethodChannel('com.feibi.imember_printer/imember_printer');

  /// 連接打印機
  static void connectPrinter() {
    _channel.invokeMethod('connectPrinter');
  }

  /// 打印庫存單
  static Future<void> printInventoryBill(List<Product> products) async {
    final List<Map<String, String?>> paramsList = [];
    for (var product in products) {
      paramsList.add({
        "name": product.name,
        "number": product.number,
      });
    }
    await _channel.invokeMethod('printInventoryBill', paramsList);
  }

  /// 打印庫存調整單
  static Future<void> printInventoryAdjustment(List<Product> products) async {
    final List<Map<String, String?>> paramsList = [];
    for (var product in products) {
      paramsList.add({
        "name": product.name,
        "differenceNumber": product.differenceNumber,
        "differenceReason": product.differenceReason,
      });
    }
    await _channel.invokeMethod('printInventoryAdjustment', paramsList);
  }

  /// 打印結算單
  static Future<void> printCheckoutBill() async {
    await _channel.invokeMethod('printCheckoutBill');
  }

  /// 打印交易明細單
  static Future<void> printTransactionDetails(List<Product> products) async {
    final List<Map<String, String?>> paramsList = [];
    for (var product in products) {
      paramsList.add({
        "name": product.name,
        "number": product.number,
        "price": product.price,
      });
    }
    await _channel.invokeMethod('printTransactionDetails', paramsList);
  }

  /// 打印出貨標籤
  static Future<void> printShippingLabel() async {
    await _channel.invokeMethod('printShippingLabel');
  }

  /// 弹出钱箱(只支持ESC/POS)
  ///
  /// number 0-2  0:1#  1:2#  2:all
  static Future<void> kickCashdrawer(int number) async {
    await _channel.invokeMethod('kickCashdrawer', number);
  }

  /// 打印POS机图片
  static Future<void> printPosImage(Uint8List bytes) async {
    await _channel.invokeMethod('printPosImage', bytes);
  }

  /// 打印标签机图片
  static Future<void> printLabelImage(Uint8List bytes) async {
    await _channel.invokeMethod('printLabelImage', bytes);
  }
}
